package logic

import (
	"context"
	"github.com/zeromicro/go-zero/core/logx"
	"mall/mallrpc/internal/svc"
	"mall/mallrpc/mallrpc"
	"mall/model/mysql"
	"mall/model/redis"
)

type UserLikeLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserLikeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserLikeLogic {
	return &UserLikeLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UserLikeLogic) UserLike(in *mallrpc.UserLikeRequest) (*mallrpc.UserLikeResponse, error) {
	// todo: add your logic here and delete this line

	var product mysql.Product
	mysql.DB.Debug().Where("id=?", in.ProductId).First(&product)

	redis.Client.ZIncrBy("productLike", 1, product.Name)

	return &mallrpc.UserLikeResponse{ProductId: int64(product.ID)}, nil
}
